package com.example.demo01;

import android.annotation.TargetApi;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.Callback;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

public class MainActivity extends AppCompatActivity {
        WebView mWebview;
        ConstraintLayout mLayout;
        WebSettings mWebSettings;
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mLayout = findViewById(R.id.mainview);
        OkGo.<String>get("http://121.199.57.242:8080/demo/isOpen")
                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if(response.body().equals("true")){
                            initView("http://121.199.57.242:8080/demo/main.html");
                        }else{
                            initView("file:///android_asset/open.html");
                        }
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        initView("file:///android_asset/error.html");
                    }
                });
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
    }





    /**
     * webView��ʼ��
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void initView(String url)
    {
        mWebview = new WebView (getApplicationContext());
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mWebview.setLayoutParams(params);

        //��ֹ����
        mWebview.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });


        mLayout.addView(mWebview);
        mWebSettings = mWebview.getSettings();
        //����֧��javaScript�ű�����
        mWebSettings.setJavaScriptEnabled(true);
        mWebSettings.setAllowFileAccessFromFileURLs(true);
        mWebSettings.setDomStorageEnabled(true);

        //����֧��js����java
        //JsToJava��js�ص����࣬Js���õ�Java������Ӧ��д������


        //����������Դ
        mWebview.loadUrl(url);


        //���ò���ϵͳ�������,ֱ����ʾ�ڵ�ǰ
        mWebview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }


            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                //6.0����ִ��
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    return;
                }
                view.loadUrl("about:blank");// �������Ĭ�ϵĴ������
                view.loadUrl("file:///android_asset/error.html");// �����Զ������ҳ��
            }

            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
                super.onReceivedHttpError(view, request, errorResponse);
                // ��������� android 6.0�ų���
                int statusCode = errorResponse.getStatusCode();
                if (404 == statusCode || 500 == statusCode) {
                    view.loadUrl("about:blank");// �������Ĭ�ϵĴ������
                    view.loadUrl("file:///android_asset/error.html");// �����Զ������ҳ��
                }
            }


            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onReceivedError(WebView webView, WebResourceRequest webResourceRequest, WebResourceError webResourceError) {
                super.onReceivedError(webView, webResourceRequest, webResourceError);
                if (webResourceRequest.isForMainFrame()) {//�Ƿ���Ϊ main frame����
                    mWebview.loadUrl("about:blank");// �������Ĭ�ϵĴ������
                    mWebview.loadUrl("file:///android_asset/error.html");// �����Զ������ҳ��
                }
            }
        });
    }
}
